package com.zygh.lz.controller.pc;

import cn.hutool.http.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zygh.lz.dao.StaffMapper;
import com.zygh.lz.entity.Staff;
import com.zygh.lz.entity.remould.Working;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@Api(tags = "支队各大队排班表")
@RestController
@RequestMapping("/RecordController")
public class RutyRosterController {

    //取当周的排班 传参date="当前年与日" _="当前年月日时分秒时间戳"
    private static String url = "http://62.66.6.6/other/no/certification/findByWeek";

    //下周 传参date="传周日年月日" _="当前年月日时分秒时间戳"
    private static String next_week = "http://62.66.6.6/other/no/certification/listLowerByDate";

    //上周 传参date="传周日年月日" _="当前年月日时分秒时间戳"
    private static String last_week = "http://62.66.6.6/other/no/certification/topListByDate";

    @Resource
    private StaffMapper staffMapper;

    @ApiOperation("本周值班表")
    @GetMapping("findByWeek")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "当天时间，例：2022-1-12", dataType = "String", required = true)
    })
    public ResultBean findByWeek(String date) {
        /*HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("date", date);
        paramMap.put("_", new Date().getTime());
        String result = HttpUtil.get(url, paramMap);
        return analysis(result);*/
        return getDutyer(date, url);
    }

    @ApiOperation("下周值班表")
    @GetMapping("listLowerByDate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "周日日期，例：2022-1-12", dataType = "String", required = true)
    })
    public ResultBean listLowerByDate(String date) {
        /*HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("date", date);
        paramMap.put("_", new Date().getTime());
        String result = HttpUtil.get(next_week, paramMap);
        return analysis(result);*/
        return getDutyer(date, next_week);
    }

    @ApiOperation("上周值班表")
    @GetMapping("topListByDate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "周日日期，例：2022-1-12", dataType = "String", required = true)
    })
    public ResultBean topListByDate(String date) {
        /*HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("date", date);
        paramMap.put("_", new Date().getTime());
        String result = HttpUtil.get(last_week, paramMap);
        return analysis(result);*/
        return getDutyer(date, last_week);
    }

    private ResultBean getDutyer(String date, String urls) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("date", date);
        paramMap.put("_", new Date().getTime());
        String result = HttpUtil.get(urls, paramMap);
        return analysis(result);
    }


    //解析返回结果
    private ResultBean analysis(String result) {
        JSONObject jsonObject = JSONObject.fromObject(result);
        Object weekNum = JSONObject.fromObject(jsonObject.get("body")).get("weekNum");
        JSONArray jsonArray = JSONObject.fromObject(jsonObject.get("body")).getJSONArray("list");
        List<Working> workingList = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<Working>>() {
        }.getType());
        for (Working working : workingList) {
            String departmentName = working.getDepartmentName();
            if (departmentName.indexOf("队") <= 0) {
                departmentName = null;
            } else {
                departmentName = departmentName.substring(0, departmentName.indexOf("队") + 1);
            }
            String mondayName = working.getMondayName();
            String tuesdayName = working.getTuesdayName();
            String wednesdayName = working.getWednesdayName();
            String thursdayName = working.getThursdayName();
            String fridayName = working.getFridayName();
            String saturdayName = working.getSaturdayName();
            String sundayName = working.getSundayName();
            Staff staffMonday = staffMapper.findStaffByNames(mondayName.trim(), departmentName);
            working.setMondayPortrait(staffMonday != null ? staffMonday.getStaffPhoto() : null);

            Staff staffTtuesday = staffMapper.findStaffByNames(tuesdayName.trim(), departmentName);
            working.setTuesdayPortrait(staffTtuesday != null ? staffTtuesday.getStaffPhoto() : null);


            Staff staffWednesday = staffMapper.findStaffByNames(wednesdayName.trim(), departmentName);
            working.setWednesdayPortrait(staffWednesday != null ? staffWednesday.getStaffPhoto() : null);

            Staff staffThursday = staffMapper.findStaffByNames(thursdayName.trim(), departmentName);
            working.setThursdayPortrait(staffThursday != null ? staffThursday.getStaffPhoto() : null);

            Staff staffFriday = staffMapper.findStaffByNames(fridayName.trim(), departmentName);
            working.setFridayPortrait(staffFriday != null ? staffFriday.getStaffPhoto() : null);

            Staff staffSaturday = staffMapper.findStaffByNames(saturdayName.trim(), departmentName);
            working.setSaturdayPortrait(staffSaturday != null ? staffSaturday.getStaffPhoto() : null);

            Staff staffSunday = staffMapper.findStaffByNames(sundayName.trim(), departmentName);
            working.setSundayPortrait(staffSunday != null ? staffSunday.getStaffPhoto() : null);

        }
        List<Map> list = new ArrayList<>();
        Map map = new HashMap();
        map.put("weekNum", weekNum);
        map.put("list", workingList);
        list.add(map);
        return ResultUtil.setOK("success", list);
    }


}
